const http = require('http');
const fs = require('fs');
let count = 0;
const server = http.createServer(function (req, res) {
  if (req.url == '/') {
    fs.readFile('./index.html', 'binary', function (err, file) {
      if (!err) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(file, 'binary');
        res.end();
      }
    });
  }
  if (req.url == '/time') {
    res.end(new Date().toLocaleString());
  }
  if (req.url == '/polling-page') {
    fs.readFile('./polling.html', 'binary', function (err, file) {
      if (!err) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(file, 'binary');
        res.end();
      }
    });
  }
  if (req.url == '/longpolling') {
    let timer = setInterval(function () {
      sendData(res, timer);
    }, 2000);
  }
  if (req.url == '/longpolling-page') {
    fs.readFile('./longpolling.html', 'binary', function (err, file) {
      if (!err) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(file, 'binary');
        res.end();
      }
    });
  }
  // 基于http-stream
  if (req.url == '/stream') {
    res.setHeader('content-type', 'multipart/octet-stream');
    let timer = setInterval(function () {
      sendRandomData(timer, res);
    }, 2000);
  }
  if (req.url == '/stream-page') {
    fs.readFile('./stream.html', 'binary', function (err, file) {
      if (!err) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(file, 'binary');
        res.end();
      }
    });
  }
  // 基于server-sent events
  if (req.url == '/evt') {
    res.writeHead(200, {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Access-Control-Allow-Origin': '*',
      Connection: 'keep-alive',
    });
    let timer = setInterval(function () {
      if (++count % 5 == 0) {
        clearInterval(timer);
        res.end('server sent event' + new Date().toLocaleString());
      } else {
        res.write('id: ' + count + '\n');
        res.write('data: ' + new Date().toLocaleString() + '\n\n');
      }
    }, 2000);
  }
  if (req.url == '/sse-page') {
    fs.readFile('./sse.html', 'binary', function (err, file) {
      if (!err) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write(file, 'binary');
        res.end();
      }
    });
  }
});
server.listen(8088, () => {
  console.log('running server on port 8088');
});
server.on('connection', function () {
  console.log('客户端连接已经建立');
});
server.on('close', function () {
  console.log('服务器被关闭');
});
//pollingtime 用随机数模拟数据是否变化
function sendData(res, timer) {
  var randomNum = Math.floor(10 * Math.random());
  // console.log(randomNum);
  if (randomNum >= 0 && randomNum <= 5) {
    clearInterval(timer);
    res.end(new Date().toLocaleString());
  }
}

function sendRandomData(timer, res) {
  var randomNum = Math.floor(10000 * Math.random());
  console.log(randomNum);

  res.write(randomNum.toString(), function () {
    if (++count % 3 == 0) {
      clearInterval(timer);
      res.end(randomNum.toString());
    }
  });
}
